package com.qingluan.widget;


import java.util.ArrayList;
import java.util.HashMap;

import com.qingluan.DataFormat.MissionTag;
import com.qingluan.Database.TempDataBase;
import com.qingluan.dd.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class DragListViewAdapter extends BaseAdapter{
	
	private Context context;
	
	
	//data arguments area
	private ArrayList<HashMap<String,String>> DATA = new ArrayList<HashMap<String,String>> ();
	private HashMap<String,Bitmap>ImageDATA = new  HashMap<String,Bitmap>();
	private ArrayList<Bitmap>upArrayList = new ArrayList<Bitmap>(); 
	
	
	//widget area
	private TextView mission_name,mission_tag,mission_time,mission_location;
//	private TextView mission_describle;
	private ImageView mission_info_picture;
	private TextView mission_up_name;
	private ImageView up_name_picture;

	public DragListViewAdapter (Context context){
		this.context = context;
	}
	public void addData(HashMap<String,String> oneCrow){
		DATA.add(oneCrow);
		
		this.notifyDataSetChanged();
	}
	public void addBitmapData(String up_name,Bitmap up_picture ){
		upArrayList.add(up_picture);
		this.notifyDataSetChanged();
	}
	public void addBitMapData(String miss_obj_id,Bitmap image){
		ImageDATA.put(miss_obj_id, image);
		this.notifyDataSetChanged();
	}
	
	public ArrayList<String> getObjIds(){
		if (DATA.isEmpty()){
			return null;
		}
		ArrayList<String> IDS = new ArrayList<String>();
		for (HashMap<String,String> item : DATA){
			IDS.add(item.get(MissionTag.MISS_OBJ_ID));
		}
		
		return IDS;
	}
	
	public boolean removeItemByObj(String obj_id){
		int position = -1;
		for (HashMap<String, String> oneItem : DATA){
			position ++;
			if (oneItem.get(TempDataBase.MISS_OBJ_ID).equals(obj_id)){
				DATA.remove(position);
				notifyDataSetChanged();
				return true;
			}
		}
		return false;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return DATA.size();
	}

	@Override
	public HashMap<String,String> getItem(int position) {
		// TODO Auto-generated method stub
		return DATA.get(position);
	}
	public Bitmap getItemPicture(int position) {
		// TODO Auto-generated method stub
		return ImageDATA.get(position);
	}
	
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		convertView = inflater.inflate(R.layout.mene_list_item, null);

//		mission_describle = (TextView)convertView.findViewById(R.id.new_mission_describe);
		mission_tag = (TextView)convertView.findViewById(R.id.new_misson_tag);
		mission_time = (TextView)convertView.findViewById(R.id.new_mission_time_info);
		mission_location = (TextView)convertView.findViewById(R.id.new_mission_location_info);
		mission_info_picture = (ImageView)convertView.findViewById(R.id.new_mission_p);
		mission_up_name = (TextView)convertView.findViewById(R.id.new_mission_up_name);
		up_name_picture = (ImageView)convertView.findViewById(R.id.new_misison_up_image);
		mission_name  = (TextView)convertView.findViewById(R.id.new_mission_name);
		
		/*
		 * set value of each widget
		 * include :
		 * 			mission name
		 * 			mission up email
		 * 			mission place name 
		 * 			mission latitude
		 * 			mission longitude 
		 * 			mission tag
		 * 			mission time
		 * 			mission picture 
		 * 
		 */
		//for search image
		String mission_obj_id = DATA.get(position).get(MissionTag.MISS_OBJ_ID);
		
		mission_name.setText(DATA.get(position).get(MissionTag.MISSION_NAME));
//		mission_describle.setText(DATA.get(position).get(MissionTag.DESCRIBE));
		mission_tag.setText(DATA.get(position).get(MissionTag.MISSION_TAG));
		mission_time.setText(DATA.get(position).get(MissionTag.TIME_S));
		mission_location.setText(DATA.get(position).get(MissionTag.PLACE_NAME));
		mission_up_name.setText(DATA.get(position).get(MissionTag.UP_NAME));
		if(!upArrayList.isEmpty()){
			up_name_picture.setImageBitmap(upArrayList.get(position));
		}
		
		if (ImageDATA != null){
			mission_info_picture.setImageBitmap(ImageDATA.get(mission_obj_id));
		}
		return convertView;
	}
	public void getMission() {
		// TODO Auto-generated method stub
		
	}

}
